package com.company.managesystem.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.company.managesystem.AppManager;
import com.company.managesystem.R;
import com.company.managesystem.common.ErrorLayout;

/**
 * 基础页面
 * Created by lqy on 2017/6/26.
 */

public abstract class BaseActivity extends AppCompatActivity {
    Context mContext;
    LinearLayout frameContent;
    private Toolbar toolbar;
    ErrorLayout error_content;

    private View contentView;
    //标志是否为MainActivity
    private boolean _isMainActivity;
    //定义一个变量，来标识是否退出
    private static boolean isExit = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_base);
        AppManager.getAppManager().addActivity(this);
        mContext = getApplicationContext();
        frameContent = (LinearLayout) super.findViewById(R.id.frame_content);
        error_content = (ErrorLayout) super.findViewById(R.id.error_content);
        error_content.setOnLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                retryToRequest();
            }
        });
        initToolbar();
        loadViewLayout();
        initView();
        initData();
    }

    @Override
    public void setContentView(int layoutResID) {
        contentView = getLayoutInflater().inflate(layoutResID, null);
        setContentView(contentView);
    }

    public void setContentView(View view) {
        frameContent.removeAllViews();
        frameContent.addView(contentView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    }

    protected abstract void loadViewLayout();

    protected abstract void initView();

    protected abstract void initData();

    //初始化toolbar
    protected void initToolbar() {
        toolbar = (Toolbar) super.findViewById(R.id.id_toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        addTitleOnToolbar();
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    //将标题栏至于中间
    protected void addTitleOnToolbar() {
        TextView textView = new TextView(mContext);
        textView.setTextSize(16);
        textView.setTextColor(Color.WHITE);
        textView.setId(R.id.toolbartitle);
        toolbar.addView(textView, new Toolbar.LayoutParams(
                Toolbar.LayoutParams.WRAP_CONTENT,
                Toolbar.LayoutParams.WRAP_CONTENT,
                Gravity.CENTER));
    }

    protected void showErrorContent() {
        Toast.makeText(BaseActivity.this, "网络连接异常，请稍后重试！", Toast.LENGTH_LONG);
//        error_content.setVisibility(View.VISIBLE);
    }

    protected void hideErrorContent() {
        error_content.setVisibility(View.INVISIBLE);
    }

    //重新加载
    protected void retryToRequest() {
        initData();
    }

    //修改toolbarTitle
    protected void setToolBarTitle(String titleString) {
        TextView textView = (TextView) toolbar.findViewById(R.id.toolbartitle);
        textView.setTextSize(16);
        textView.setText(titleString);
    }

    //修改toolbar回退按钮
    protected void setToobarBackIcon(int resId) {
        toolbar.setNavigationIcon(resId);
    }

    //设置是否为MainActivity标志
    protected void setMainActivityFlag(boolean isMainActivity) {
        this._isMainActivity = isMainActivity;
    }

    private static Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            isExit = false;
        }
    };

    private void exit() {
        if (_isMainActivity) {
            if (!isExit) {
                isExit = true;
                Toast.makeText(getApplicationContext(), "再按一次后退键退出程序",
                        Toast.LENGTH_SHORT).show();
                // 利用handler延迟发送更改状态信息
                mHandler.sendEmptyMessageDelayed(0, 2000);
            } else {
                this.finish();

            }
        } else {
            this.finish();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
